package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"paper-manage/model"
)

var One = func(ctx *gin.Context) {
	form := new(FormGet)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}

	switch form.Item {
	case "paper":
		paper := &model.Paper{
			Id: form.Id,
		}
		err := paper.OneById()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, paper)

	case "section":
		section := &model.Section{
			Id: form.Id,
		}
		err := section.OneById()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, section)

	case "question":
		question := &model.Question{
			Id: form.Id,
		}
		err := question.OneById()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, question)

	default:
		ctx.Status(http.StatusBadRequest)
	}
}

var PicList = func(ctx *gin.Context) {
	form := new(FormGet)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	question := &model.Question{}
	qs, err := question.PicList(form.PageNum, form.PageSize)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	total, _ := question.CountPic()
	ctx.JSON(http.StatusOK, gin.H{
		"total":     total,
		"questions": qs,
	})
}

var List = func(ctx *gin.Context) {
	form := new(FormGet)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}

	switch form.Item {
	case "paper":
		paper := &model.Paper{}
		ps, err := paper.Page(form.PageNum, form.PageSize, form.Desc, form.Order, form.NameLike)
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		total, _ := paper.CountAll()
		ctx.JSON(http.StatusOK, gin.H{
			"total":  total,
			"papers": ps,
		})
	case "section":
		section := &model.Section{}
		if form.Pid > 0 {
			p := &model.Paper{
				Id: form.Pid,
			}
			if err := p.OneById(); err != nil {
				ctx.AbortWithError(http.StatusBadRequest, err)
				return
			}

			ss, err := section.SomeDesc(p.Sids)
			if err != nil {
				ctx.AbortWithError(http.StatusBadRequest, err)
				return
			}

			ctx.JSON(http.StatusOK, gin.H{
				"total":    len(p.Sids),
				"sections": ss,
			})
			return
		}
		ss, err := section.Page(form.PageNum, form.PageSize, form.Desc, form.Order)
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		total, _ := section.CountAll()
		ctx.JSON(http.StatusOK, gin.H{
			"total":    total,
			"sections": ss,
		})
	case "question":
		question := &model.Question{}
		if form.Sid > 0 {
			s := &model.Section{
				Id: form.Sid,
			}
			if err := s.OneById(); err != nil {
				ctx.AbortWithError(http.StatusBadRequest, err)
				return
			}

			qs, err := question.SomeDesc(s.Qids)
			if err != nil {
				ctx.AbortWithError(http.StatusBadRequest, err)
				return
			}
			ctx.JSON(http.StatusOK, gin.H{
				"total":     len(s.Qids),
				"questions": qs,
			})
			return
		}
		qs, err := question.Page(form.PageNum, form.PageSize, form.Desc, form.Order)
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		total, _ := question.CountAll()
		ctx.JSON(http.StatusOK, gin.H{
			"total":     total,
			"questions": qs,
		})
	default:
		ctx.Status(http.StatusBadRequest)
	}
}

var Create = func(ctx *gin.Context) {
	form := new(FormEdit)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}

	switch form.Item {
	case "paper":
		paper := &form.Paper
		err := paper.Create()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, paper)

	case "section":
		section := &form.Section
		err := section.Create()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}

		paper := &model.Paper{
			Id: form.Pid,
		}
		if err := paper.OneById(); err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		if err := paper.AddSection(section.Id); err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		ctx.JSON(http.StatusOK, section)

	case "question":
		question := &form.Question
		err := question.Create()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}

		section := &model.Section{
			Id: form.Sid,
		}
		if err := section.OneById(); err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		if err := section.AddQuestion(question.Id); err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		ctx.JSON(http.StatusOK, question)

	default:
		ctx.Status(http.StatusBadRequest)
	}
}

var Change = func(ctx *gin.Context) {
	form := new(FormEdit)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}

	switch form.Item {
	case "paper":
		//paper := &form.Paper
		//err := paper.Create()
		//if err != nil {
		//	ctx.AbortWithError(http.StatusBadRequest, err)
		//	return
		//}
		//ctx.JSON(http.StatusOK, paper)

	case "section":
		//section := &form.Section
		//err := section.Create()
		//if err != nil {
		//	ctx.AbortWithError(http.StatusBadRequest, err)
		//	return
		//}
		//ctx.JSON(http.StatusOK, section)

	case "question":
		question := &form.Question
		err := question.Update()
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, question)

	default:
		ctx.Status(http.StatusBadRequest)
	}
}

var Search = func(ctx *gin.Context) {
	form := new(FormSearch)
	err := ctx.Bind(form)
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}

	switch form.Item {
	case "paper":

	case "section":

	case "question":
		question := &model.Question{}
		qs, err := question.SomeLike(form.Search)
		if err != nil {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
		ctx.JSON(http.StatusOK, gin.H{
			"questions": qs,
			"total":     len(qs),
		})

	default:
		ctx.Status(http.StatusBadRequest)
	}
}
